package com.soundai.skill.helloworld.Controller;

import com.soundai.skill.helloworld.HttpHelper.HttpClientHelper;
import com.soundai.skill.helloworld.baiduApi.AipNlpClass;
import com.soundai.skill.helloworld.entity.ReqArgs;
import com.soundai.skill.helloworld.helper.TextConvertNumber;
import com.soundai.skill.helloworld.socket.SocketClient;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;


/**
 * TODO ：处理调用接口方法控制器
 * author：yanpeixin
 * date  ：2019-07-03
 */

 @Controller
 @RestController
 @RequestMapping("/v1/common")
public class ConmonController extends HttpServlet {


    @PostMapping(value = "/run")
    public String method(@RequestBody ReqArgs args){
        String retrunMsg = null;
        //判断领域
        String domainTag = args.domainTag;
        switch (domainTag){
            case "智慧家庭":
                retrunMsg = runToHome(args);
                break;
            case "音乐":
                break;
            case "天气":
                break;
            case "故事":
                break;
            case "商城":
                retrunMsg = runToShopping(args);
                break;
            case "购物":
                break;
            default:
                break;
        }
        return retrunMsg;
    }


    /**
     * 处理购物文本串，并解析处理
     * @param args
     * @return
     */
    public  String runToShopping(ReqArgs args){
        org.json.JSONObject obj =  AipNlpClass.handelString("购买五十瓶可乐");
        JSONArray arr = null;
        try {
            arr = JSONArray.fromObject(obj.get("items").toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String vPosItem = "";
        for (int i=0;i<arr.size();i++){
             JSONObject myjObject = JSONObject.fromObject(arr.get(i));
            String pos = myjObject.get("pos").toString();
            if(pos.equals("v")){ //动词
                vPosItem = myjObject.get("item").toString();
            }else if(pos.equals("m")){ //量词
                String[] basicWords = myjObject.get("basic_words").toString().replace("[","").replace("]","").split(",");
                for(int j=0;j<basicWords.length;j++){
                    int num = TextConvertNumber.chineseNumber2Int(basicWords[j]);
                    //System.out.println("转换数字为："+num);
                }
            }else  if(pos.equals("n") || pos.equals("vn") || pos.equals("nw")) { //名词等
                String a = myjObject.get("item").toString();
                String retm = HttpClientHelper.doGet("http://localhost:44306/api/Product/GetProductList?pName="+a);
                //System.out.println("查询商品："+retm);
                toOrder("",retm);
            }
            String a = "";
        }



        //String items = obj.get("items").toString();
        //JSONObject objItems = JSONObject.fromObject(obj);
        //String items = objItems.get("items").toString();
        //String ss = "123123";

        return  "";
    }

    /**
     * 选购商品下单
     * @param userId
     * @param productInfo
     * @return
     */
    public String toOrder(String userId,String productInfo){
       JSONObject obj = JSONObject.fromObject(productInfo.replace("\r\n",""));
        String data = obj.get("Data").toString();
        //JSONArray arr = new JSONArray(data.toString());

        JSONArray resultJson = JSONArray.fromObject(data);
        //JSONObject js = JSONObject.fromObject(str);

        String skuId = "";
        for (int i=0;i<resultJson.size();i++) {
            JSONObject myjObject = JSONObject.fromObject(resultJson.get(i));
            //org.json.JSONObject myjObject = arr.getJSONObject(1);
             skuId = myjObject.get("SkuId").toString();
        }
        String createOrder = HttpClientHelper.doPost("http://localhost:44306/api/Order/SubmitOrder?productSku="+skuId+"&buyAmount=1&buyUserId=77777","");
        return  createOrder;
    }

    /**
     * 智慧家庭
     * @param args
     * @return
     */
    public String runToHome(ReqArgs args){
        String returnMsg = null;
        //执行动作标签
        String strAction = args.actionTag;
        String connUser = args.userId;
        String serverCode = null;
        String deviceId = null;
        String roomId = null;
        String password = null;
        String reqArgs = args.requetArgs;

        JSONObject jsonObject = new JSONObject();
        try
        {
            if(!StringUtils.isEmpty(reqArgs))
            {
                //log.writeFile("","请求参数："+reqArgs+",请求时间："+log.getCurrentYYYYMMDDHHMMSS());
                jsonObject = JSONObject.fromObject(reqArgs);
                serverCode = jsonObject.get("serverCode").toString();
            }

            switch (strAction){
                case "注册":
                    String regCode = jsonObject.get("regCode").toString();
                    //returnMsg = SocketClient.regHome(serverCode,regCode);//用户，开灯状态码，设备ID
                    break;
                case "开灯":
                case "开启":
                    deviceId = jsonObject.get("deviceId").toString();
                    //returnMsg = SocketClient.requestHomeSocket(serverCode,"8;8135",deviceId);//用户，开灯状态码，设备ID
                    break;
                case "关灯":
                case "关闭":
                    deviceId = jsonObject.get("deviceId").toString();
                    //returnMsg = SocketClient.requestHomeSocket(serverCode,"8;8145",deviceId);//用户，开灯状态码，设备ID
                    break;

                case "窗帘暂停":
                    break;
                case "查询设备":
                    if (jsonObject.get("roomId")!=null)
                        roomId = jsonObject.get("roomId").toString();
                    else
                        roomId = "0";
                    String presize = "0";//jsonObject.get("presize").toString();
                    if (jsonObject.get("presize")!=null)
                        presize = jsonObject.get("presize").toString();
                    String lastsize = "20";//jsonObject.get("lastsize").toString();
                    if (jsonObject.get("lastsize")!=null)
                        lastsize = jsonObject.get("lastsize").toString();
                    String devtype = "Panel_Zigbee,Panel_SingleZigbee,Panel_Two_Control";//jsonObject.get("devtype").toString();
                    if (jsonObject.get("devtype")!=null)
                        devtype = jsonObject.get("devtype").toString();
                    roomId=roomId+";"+devtype+";"+presize+","+lastsize;
                    //roomId="0;Panel_Zigbee;0;10";
                    //returnMsg = SocketClient.requestHomeSocket(serverCode,"8;8551",roomId);//用户，开灯状态码，设备ID
                    break;
                case "房间列表":
                    //用户，开灯状态码，登录名
                    String loginName = jsonObject.get("loginName").toString();
                    //returnMsg =  SocketClient.requestHomeSocket(serverCode,"8;835",loginName);
                    break;
                case "场景":
                    //场景状态参数 默认查询：all
                    String senceCode = jsonObject.get("senceCode").toString();
                    //returnMsg =  SocketClient.requestHomeSocket(serverCode,"9;923",senceCode);
                    break;
                case "场景开关":
                    String openStatu = jsonObject.get("openStatu").toString();
                    String taskid = jsonObject.get("taskid").toString();
                    String runCode = "";
                    if(openStatu.equals("关")) {
                        runCode = "9;929;all;3;" + taskid;
                    }
                    else {
                        String runid = jsonObject.get("runid").toString();
                        runCode = "9;912;all;2;" + runid + ";" + taskid;
                    }
                    //returnMsg =  SocketClient.requestHomeSocket(serverCode,runCode,taskid);
                    break;

                default:
                    break;
            }
            return returnMsg;
        } catch (Exception e)
        {
            //log.writeFile("","出错："+e.toString()+",出错时间："+log.getCurrentYYYYMMDDHHMMSS());
            return "[]";
        }


    }


}
